<?php
   include_once "mysql_connect.php";
	
	class Functions
	{
		public static function getData($nameTable, $fields, $where="", $additionalSql="")
		{
			$tam = count($fields);		
			$sql = "	SELECT ".$fields[0];

			for($i=1; $i < $tam; $i++)
			   $sql.= ", ".$fields[$i];
				
			$sql.=" FROM ".$nameTable;
			if( isset($where) && ($where != ""))
			   $sql.=" WHERE ".$where;
				
			if( isset($additionalSql) && ($additionalSql != ""))
			   $sql.=" ".$additionalSql;		
				
			$result= mysql_query($sql);
			if( $result )
			{
				$i=0;
			   while($linha= mysql_fetch_array($result))
				{
					for($j=0; $j < $tam; $j++)
						$mat[$i][$j]= $linha[$j];
					$i++;
				}
			}

			return $mat;
		}

		public static function execSql($sql)
		{
			return mysql_query($sql);
		}
		
		public static function getDataSql($sql)
		{
			$result= mysql_query($sql);
			$mat = array();
			if( ($result) && (mysql_num_rows($result) > 0) )
			{
				$i=0;
			   /*while($linha= mysql_fetch_array($result))
				{
					//echo $sql;
					for($j=0; $j < count($linha); $j++)
						$mat[$i][$j]= $linha[$j];
					$i++;
				}*/
				
				//
				$keys=array_keys(mysql_fetch_array($result, MYSQL_ASSOC));
				mysql_data_seek($result, 0);				
			   while($linha= mysql_fetch_array($result, MYSQL_NUM))
				{							
			      for($j=0; $j < count($linha); $j++)
					{
			         $mat[$i][$keys[$j]]= $linha[$j];
			      }	
					$i++;
				}				
			}
			return $mat;
		}

		public static function getValue($sql)
		{
			$result= mysql_query($sql);
			$value = 0;
			if( $result )
			{
			   $linha= mysql_fetch_array($result);
				$value= $linha[0];
			}
			return $value;
		}
		
		public static function getArraySql($sql)
		{
			$result= mysql_query($sql);
			$array= array();
			if( $result )
			{
			   if($linha= mysql_fetch_array($result, MYSQL_NUM))
				{
					mysql_data_seek($result, 0);
					$keys=array_keys(mysql_fetch_array($result, MYSQL_ASSOC));								
			      for($i=0; $i < count($linha); $i++)
					{
			         $array[$keys[$i]]= $linha[$i];
			      }						
				}
			}
			return $array;
		}
		
		public static function makeCombo($name, $mat, $selected=0, $width=250, $tags= "")
		{
			$options= "";
			for($i=0; $i < count($mat); $i++)
			{
			   $options.= "<option value='".$mat[$i][0]."'";
				if( $mat[$i][0] == $selected)
				{
				   $options.=" selected	";
				}
				$options.=">".$mat[$i][1]."</option>";	
				
			}
			
			$combo= "<select name='".$name."' STYLE='width: ".$width."px' ".$tags.">".$options."</select>";
			
			return $combo;
		}
		
		public static function formatDate($value, $format="yyyy/mm/dd")
		{
			$array= split("/",$value);
			$day =  $array[0];
			$month= $array[1];
			$year = $array[2];
			
			if( $format == "yyyy/mm/dd")
				return $year."/".$month."/".$day;
		}
		
		public static function convertMiliToTime($mili){
         if( $mili == 0)
            return "00:00:00";
         
			$seconds = (int) ($mili / 1000);
			$aux= "";
			
			$hours = (int) ($seconds / 3600);
			$seconds = (int)($seconds % 3600);
			if( $hours < 10)
				$aux= "0";
			$aux.= $hours.":";
			
			$minutes = (int) ($seconds / 60);
			$seconds = (int) ($seconds%60);	
			if( $minutes < 10)	
				$aux.="0";
			$aux.= $minutes.":";
			
			if( $seconds  < 10 )
				$aux.="0";
			$aux.= $seconds;
			
			return $aux;
		}
		
		public static function convertMiliToDate($mili){
			$date = date_create();
			$mili = (int) $mili/1000;
			date_timestamp_set($date, $mili);
			
			return date_format($date, 'd/m/Y');
		}		
	
		//Level 1-Sedentary, 2-Active, 3-Atlhete
		public static function generateZones($old, $level_conditioning, $min_bpm){
			$blue = array();
			$yellow = array();
			$green = array();
			$red = array();
			
			$result = array();
			$max_bpm= intval(208 - (0.7 * $old));
			
			if( $level_conditioning == 1){
				if( $min_bpm == 0)
					$min_bpm = 80;
				$delta= intval($max_bpm - $min_bpm);
				
				$blue["bpm_min"]= intval($delta * 0.5 + $min_bpm);
				$blue["bpm_max"]= intval($delta * 0.7 + $min_bpm);
				$blue["pace_min"]=385000; //6:25
				$blue["pace_max"]=698000; //11:38
				$blue["speed_min"]=1.433; //5.16 km/h
				$blue["speed_max"]=2.597; //9.70 km/h
				
				
				$green["bpm_min"]= $blue["bpm_max"] + 1;
				$green["bpm_max"]= intval($delta * 0.8 + $min_bpm);
				$green["pace_min"]=286000; //4:46
				$green["pace_max"]=384000; //6:24
				$green["speed_min"]=2.598;//9.71
				$green["speed_max"]=3.497;//12.59		


				$yellow["bpm_min"]=$green["bpm_max"] + 1;
				$yellow["bpm_max"]=intval($delta * 0.9 + $min_bpm);
				$yellow["pace_min"]=246000; //4:06
				$yellow["pace_max"]=285000; //4:45
				$yellow["speed_min"]=3.498; //12.60
				$yellow["speed_max"]=4.063;	//14.63

				$red["bpm_min"]= $yellow["bpm_max"] + 1;
				$red["bpm_max"]= $max_bpm;
				$red["pace_min"]=186000; //3:06
				$red["pace_max"]= 245000; //4:05
				$red["speed_min"]=4.064; //14.64
				$red["speed_max"]=5.375; //19.35					
			}
			else if($level_conditioning == 2){
				if( $min_bpm == 0)
					$min_bpm = 60;
				$delta= intval($max_bpm - $min_bpm);
				
				$blue["bpm_min"]= intval($delta * 0.5 + $min_bpm);
				$blue["bpm_max"]= intval($delta * 0.7 + $min_bpm);
				$blue["pace_min"]=385000; //6:25
				$blue["pace_max"]=698000; //11:38
				$blue["speed_min"]=1.433; //5.16 km/h
				$blue["speed_max"]=2.597; //9.70 km/h
				
				
				$green["bpm_min"]= $blue["bpm_max"] + 1;
				$green["bpm_max"]= intval($delta * 0.8 + $min_bpm);
				$green["pace_min"]=286000; //4:46
				$green["pace_max"]=384000; //6:24
				$green["speed_min"]=2.598;//9.71
				$green["speed_max"]=3.497;//12.59		


				$yellow["bpm_min"]=$green["bpm_max"] + 1;
				$yellow["bpm_max"]=intval($delta * 0.9 + $min_bpm);
				$yellow["pace_min"]=246000; //4:06
				$yellow["pace_max"]=285000; //4:45
				$yellow["speed_min"]=3.498; //12.60
				$yellow["speed_max"]=4.063;	//14.63

				$red["bpm_min"]= $yellow["bpm_max"] + 1;
				$red["bpm_max"]= $max_bpm;
				$red["pace_min"]=186000; //3:06
				$red["pace_max"]= 245000; //4:05
				$red["speed_min"]=4.064; //14.64
				$red["speed_max"]=5.375; //19.35				
			}
			else{
				if( $min_bpm == 0)
					$min_bpm = 50;
				$delta= intval($max_bpm - $min_bpm);
				
				$blue["bpm_min"]= intval($delta * 0.5 + $min_bpm);
				$blue["bpm_max"]= intval($delta * 0.7 + $min_bpm);
				$blue["pace_min"]=385000; //6:25
				$blue["pace_max"]=698000; //11:38
				$blue["speed_min"]=1.433; //5.16 km/h
				$blue["speed_max"]=2.597; //9.70 km/h
				
				
				$green["bpm_min"]= $blue["bpm_max"] + 1;
				$green["bpm_max"]= intval($delta * 0.8 + $min_bpm);
				$green["pace_min"]=286000; //4:46
				$green["pace_max"]=384000; //6:24
				$green["speed_min"]=2.598;//9.71
				$green["speed_max"]=3.497;//12.59		


				$yellow["bpm_min"]=$green["bpm_max"] + 1;
				$yellow["bpm_max"]=intval($delta * 0.9 + $min_bpm);
				$yellow["pace_min"]=246000; //4:06
				$yellow["pace_max"]=285000; //4:45
				$yellow["speed_min"]=3.498; //12.60
				$yellow["speed_max"]=4.063;	//14.63

				$red["bpm_min"]= $yellow["bpm_max"] + 1;
				$red["bpm_max"]= $max_bpm;
				$red["pace_min"]=186000; //3:06
				$red["pace_max"]= 245000; //4:05
				$red["speed_min"]=4.064; //14.64
				$red["speed_max"]=5.375; //19.35				
			}
		
			$result["blue"]= $blue;
			$result["yellow"]= $yellow;
			$result["green"]= $green;
			$result["red"]= $red;
			
			return $result;
		}
	}
?>
